package com.example.shiwu;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.shiwu.adapter.MySimpleAdapter;
import com.example.shiwu.adapter.MySimpleChangeAdapter;
import com.example.shiwu.db.model.Items;
import com.example.shiwu.service.ItemsService;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ItemListChangeActivity extends AppCompatActivity{
    private RefreshLayout mRefreshLayout;
    private ListView listView;
    private MySimpleChangeAdapter myAdapter;

    private ItemsService itemsService;

    private Map<String, Object> showitem;
    private ArrayList<Items> itemLisAll = new ArrayList<Items>();
    private int currentPage = 0;
    private int pageSize = 2;
    private int totalSize = 0;

    public ListView editSb;

    public TextView title_text,title_seting,items_detail;
    public ImageView title_back;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list_change);

        title_back = findViewById(R.id.title_back);
        title_text = findViewById(R.id.title_text);
        title_seting = findViewById(R.id.title_setting);
//        items_detail = findViewById(R.id.items_change_detail);
//        items_detail.setVisibility(View.INVISIBLE);

        title_text.setText("项目列表");
        title_seting.setText("切换列表");
        itemsService = new ItemsService();
        //itemLis = new ArrayList<Items>();
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //ItemListChangeActivity.this.finish();
                Intent intent = new Intent(view.getContext(),ItemInfoListActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                //finish();
            }
        });

        this.getItemsData();

        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() { //下拉刷新
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getItemsDataHeaderPull();
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });

        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { //上拉加载更多
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                getItemsDataFooterPull();
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });


        listView = (ListView) findViewById(R.id.list_change_view1);
//        if (null != listView) {
//            ListAdapter listAdapter = listView.getAdapter();
//            if (listAdapter == null) {
//                return;
//            }
//            int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
//            final float scale = this.getResources().getDisplayMetrics().density;
//            ViewGroup.LayoutParams params = listView.getLayoutParams();
//            if (null != params) {
//                params.height = (int)(screenHeight * scale)-80;
//                System.out.println("==========="+params.height);
//            }
//        }
    }


    private void getItemsData(){
        itemsService = new ItemsService();
        List<Items> itemLis = itemsService.getItemsList(new Items(),(currentPage*pageSize),pageSize);
        if(itemLis.size()>0){
            currentPage ++;
            itemLisAll.addAll(itemLis);
            //创建一个simpleAdapter
            myAdapter = new MySimpleChangeAdapter(getApplicationContext(), itemLisAll, R.layout.item_list_item_change);

            listView = findViewById(R.id.list_change_view1);
            listView.setAdapter(myAdapter);

            List<Items> itemLisSize = itemsService.getItems(new Items());
            totalSize = itemLisSize.size();
        }

    }

    private void getItemsDataHeaderPull(){

        currentPage =0;
        itemLisAll= null;
        itemLisAll = new ArrayList<Items>();
        itemsService = new ItemsService();
        List<Items> itemLis = itemsService.getItemsList(new Items(),(0*pageSize),pageSize);
        if(itemLis.size()>0){
            itemLisAll.addAll(itemLis);
            //创建一个simpleAdapter
            myAdapter = new MySimpleChangeAdapter(getApplicationContext(), itemLisAll, R.layout.item_list_item_change);

            listView = findViewById(R.id.list_change_view1);
            listView.setAdapter(myAdapter);

            currentPage ++;
            myAdapter.notifyDataSetChanged();
        }
    }
    private void getItemsDataFooterPull(){

        itemsService = new ItemsService();
        List<Items> itemLis = itemsService.getItemsList(new Items(),(currentPage*pageSize),pageSize);
        if(itemLis.size()>0){
            itemLisAll.addAll(itemLis);
            //创建一个simpleAdapter
            myAdapter = new MySimpleChangeAdapter(getApplicationContext(), itemLisAll, R.layout.item_list_item_change);

            listView = findViewById(R.id.list_change_view1);
            listView.setAdapter(myAdapter);

            currentPage ++;
            myAdapter.notifyDataSetChanged();
        }
    }

}